#coding:utf-8

import jieba

from django import template
from app.constant import SensitiveWord

register = template.Library()

@register.filter
def sample_check(value):
    # 传进来的是字符串,拆分成词, 使用 jieba
    cut_message = jieba.lcut(value)
    # 判断拆分后的词和我们的词库是否有重复
    check = list(set(cut_message) & set(SensitiveWord))

    if len(check) != 0:
        return '该消息有违禁词汇'
    return value


@register.filter(name='deep_check_message')
def deep_check(value):
    cut_message = jieba.lcut(value)

    new_message = []

    for word in cut_message:
        if word in SensitiveWord:
            new_message.append('*')
        else:
            new_message.append(word)
    
    if new_message:
        return ''.join(new_message)
    return value